import vgamepad as vg
import time

class XBox360GamepadController:
    def __init__(self):
        self.gamepad = vg.VX360Gamepad()
        print("正在创建虚拟XBox360游戏手柄...")
    
    def press_a(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
        self.gamepad.update()
    
    def release_a(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
        self.gamepad.update()
    
    def press_b(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
        self.gamepad.update()
    
    def release_b(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_B)
        self.gamepad.update()
    
    def press_x(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
        self.gamepad.update()
    
    def release_x(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_X)
        self.gamepad.update()
    
    def press_y(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_Y)
        self.gamepad.update()
    
    def release_y(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_Y)
        self.gamepad.update()
    
    def press_lb(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER)
        self.gamepad.update()
    
    def release_lb(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_LEFT_SHOULDER)
        self.gamepad.update()
    
    def press_rb(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_SHOULDER)
        self.gamepad.update()
    
    def release_rb(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_RIGHT_SHOULDER)
        self.gamepad.update()
    
    def lt_float(self, value_float):
        self.gamepad.left_trigger_float(value_float=value_float)
        self.gamepad.update()
    
    def rt_float(self, value_float):
        self.gamepad.right_trigger_float(value_float=value_float)
        self.gamepad.update()
    
    def left_joystick_float(self, x_value_float, y_value_float):
        self.gamepad.left_joystick_float(x_value_float=x_value_float, y_value_float=y_value_float)
        self.gamepad.update()
    
    def right_joystick_float(self, x_value_float, y_value_float):
        self.gamepad.right_joystick_float(x_value_float=x_value_float, y_value_float=y_value_float)
        self.gamepad.update()
    
    def press_dpad_up(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
        self.gamepad.update()
    
    def release_dpad_up(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP)
        self.gamepad.update()
    
    def press_dpad_down(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
        self.gamepad.update()
    
    def release_dpad_down(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN)
        self.gamepad.update()
    
    def press_dpad_left(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def release_dpad_left(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def press_dpad_right(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def release_dpad_right(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def press_dpad_northwest(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def release_dpad_northwest(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def press_dpad_southwest(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def release_dpad_southwest(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)
        self.gamepad.update()
    
    def press_dpad_northeast(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def release_dpad_northeast(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_UP | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def press_dpad_southeast(self):
        self.gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def release_dpad_southeast(self):
        self.gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_DOWN | vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_RIGHT)
        self.gamepad.update()
    
    def reset(self):
        self.gamepad.reset()
        self.gamepad.update()

# 示例使用
if __name__ == "__main__":
    controller = XBox360GamepadController()
    
    # 按下A键唤醒设备
    controller.press_a()
    time.sleep(0.5)
    controller.release_a()
    time.sleep(0.5)
    
    # 控制左边的摇杆
    controller.left_joystick_float(-0.5, 0.0)  # 向左移动
    time.sleep(1.0)
    controller.left_joystick_float(0.0, 0.0)  # 回中
    time.sleep(1.0)
    
    # 控制右边的摇杆
    controller.right_joystick_float(0.5, -0.5)  # 向右下方移动
    time.sleep(1.0)
    controller.right_joystick_float(0.0, 0.0)  # 回中
    time.sleep(1.0)
    
    # 按下方向键
    controller.press_dpad_up()
    time.sleep(1.0)
    controller.release_dpad_up()
    time.sleep(1.0)
    
    controller.press_dpad_down()
    time.sleep(1.0)
    controller.release_dpad_down()
    time.sleep(1.0)
    
    controller.press_dpad_left()
    time.sleep(1.0)
    controller.release_dpad_left()
    time.sleep(1.0)
    
    controller.press_dpad_right()
    time.sleep(1.0)
    controller.release_dpad_right()
    time.sleep(1.0)
    
    # 按下组合方向键
    controller.press_dpad_northwest()
    time.sleep(1.0)
    controller.release_dpad_northwest()
    time.sleep(1.0)
    
    controller.press_dpad_southwest()
    time.sleep(1.0)
    controller.release_dpad_southwest()
    time.sleep(1.0)
    
    controller.press_dpad_northeast()
    time.sleep(1.0)
    controller.release_dpad_northeast()
    time.sleep(1.0)
    
    controller.press_dpad_southeast()
    time.sleep(1.0)
    controller.release_dpad_southeast()
    time.sleep(1.0)
    
    # 按下右边的XYAB按钮
    controller.press_x()
    time.sleep(0.5)
    controller.release_x()
    time.sleep(0.5)
    
    controller.press_y()
    time.sleep(0.5)
    controller.release_y()
    time.sleep(0.5)
    
    controller.press_a()
    time.sleep(0.5)
    controller.release_a()
    time.sleep(0.5)
    
    controller.press_b()
    time.sleep(0.5)
    controller.release_b()
    time.sleep(0.5)
    
    # 按下LT RT LB RB按钮
    controller.lt_float(0.5)  # LT半按
    time.sleep(1.0)
    controller.lt_float(0.0)  # 松开
    time.sleep(1.0)
    
    controller.rt_float(0.5)  # RT半按
    time.sleep(1.0)
    controller.rt_float(0.0)  # 松开
    time.sleep(1.0)
    
    controller.press_lb()  # LB按下
    time.sleep(1.0)
    controller.release_lb()  # 松开
    time.sleep(1.0)
    
    controller.press_rb()  # RB按下
    time.sleep(1.0)
    controller.release_rb()  # 松开
    time.sleep(1.0)
    
    # 重置游戏手柄
    controller.reset()



